package org.mch.func.str.edit;

import java.awt.event.ActionEvent;

import javax.swing.text.BadLocationException;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;
import org.mch.swingm.core.MFileChooser;

/**
 * 描述：字符串编辑；<BR/>
 *
 * @author MCH；Date：2008-06-18；<BR/>
 *
 */
public class ClientEvent extends FuncEvent {
	private static final long serialVersionUID = 1432304718506228120L;

	MFileChooser fileChooser;

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_execute()) {
			execute();
		}
	}

	public void execute() {
		if (getPanel().getRadio_01().isSelected()) {
			onRadio01();
		} else if (getPanel().getRadio_02().isSelected()) {
			onRadio02();
		}
	}

	private void onRadio02() {
		String text02 = getPanel().getText_02().getText();

		int lineCount = getPanel().getText_str().getLineCount();// 确定文本区中所包含的行数

		for (int i = 0; i < lineCount; i++) {
			try {
				int lineEnd = getPanel().getText_str().getLineEndOffset(i);// 确定给定行结尾处的偏移量

				getPanel().getText_str().insert(text02, lineEnd - 1);

			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
	}

	private void onRadio01() {
		String text01 = getPanel().getText_01().getText();

		int lineCount = getPanel().getText_str().getLineCount();// 确定文本区中所包含的行数

		for (int i = 0; i < lineCount; i++) {
			try {
				int lineStart = getPanel().getText_str().getLineStartOffset(i);// 确定给定行起始处的偏移量

				getPanel().getText_str().insert(text01, lineStart);

			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}